#include <SelectorManagerOsg.h>
#include <osgwTools/InsertRemove.h>


SelectorManagerOsg* SelectorManagerOsg::_instance = NULL;
SelectorManagerOsg::SelectorManagerOsg()
{
    //m_outLineNode = new osgFX::Outline;
    //m_outLineNode->setWidth(8);
    //m_outLineNode->setColor(osg::Vec4(1,1,0,1));

    //m_selectedNode = new osg::Group();
    m_isAdding = false;
    m_isCanceling = false;
}
SelectorManagerOsg::~SelectorManagerOsg()
{

}

SelectorManagerOsg* SelectorManagerOsg::instance()
{
	if (_instance == NULL)
	{
        _instance = new SelectorManagerOsg();
	}
	return _instance;

}

void SelectorManagerOsg::addSelectedNode( osg::ref_ptr<osg::Node> node_ptr )
{
    /*
    if(m_isCanceling == true) return;*/
//std::cout<<"select start"<<std::endl;

    m_isAdding = true;
	if (node_ptr == NULL)
	{
		return;
	}

//    if( m_selectedNode->getChildIndex(node_ptr.get()) != m_selectedNode->getNumChildren())
//    {
//        return;
//    }

    for(int i = 0 ; i < m_selectedNode.size(); i++)
    {
        if(m_selectedNode[i].get() ==  node_ptr.get())
        {
            return;
        }
    }

    //m_outLineNode->addChild(node_ptr);//bug,may drop down the FPS

    osg::ref_ptr<osgFX::Outline> newOutLine = new osgFX::Outline();
    newOutLine->setWidth(2);
    newOutLine->setColor(osg::Vec4(1,1,0,1));

    m_outLineNodeVector.push_back(newOutLine);
    osgwTools::insertAbove(node_ptr, newOutLine);

    //m_selectedNode->addChild(node_ptr);
    m_selectedNode.push_back(node_ptr);

    //std::cout<<"outLineNodeVector.Size = "<<m_outLineNodeVector.size()<<std::endl;
    //std::cout<<"m_selectedNode.children = "<<m_selectedNode->getNumChildren()<<std::endl;
    m_isAdding = false;
//std::cout<<"select end"<<std::endl;
}

void SelectorManagerOsg::addSingleSelectedNode( osg::ref_ptr<osg::Node> node_ptr )
{
	cancelSelectedNode();
	if (node_ptr == NULL)
	{
		//SceneViewerFunctions::nodeDoLuaFiles(this);
		return;
	}
	else
	{
        addSelectedNode(node_ptr);
	}
	
	
}


int SelectorManagerOsg::getSelectedNodeSize()
{
    return m_selectedNode.size();
}


osg::ref_ptr<osg::Node> SelectorManagerOsg::getSelectedNode( int index )
{
    if (index < m_selectedNode.size())
	{
        return m_selectedNode[index];
	}
	return NULL;
}


void SelectorManagerOsg::handleSelectedNode()
{
    /*
	_selected_node_stateset_vector.clear();
	int i;
	for (i = 0 ; i < _selected_node_vector.size(); i++)
	{
		osg::ref_ptr<osg::StateSet> state_ptr = _selected_node_vector[i]->getOrCreateStateSet();
		_selected_node_stateset_vector.push_back(state_ptr);

		
		osg::Material* material = new osg::Material;
		material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1,0,0,0.5) );
		//state_ptr->setAttributeAndModes( material , osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
		state_ptr = new osg::StateSet;
		state_ptr->setAttributeAndModes( material , osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE );
		_selected_node_vector[i]->setStateSet(state_ptr);
	}
    */
	
}

void SelectorManagerOsg::cancelSelectedNode()
{
    /*
	int i;
	for (i = 0 ; i < _selected_node_vector.size(); i++)
	{
		osg::ref_ptr<osg::StateSet> state_ptr = _selected_node_stateset_vector[i];
		_selected_node_vector[i]->setStateSet(state_ptr);
	}
	_selected_node_vector.clear();
	_selected_node_stateset_vector.clear();
*/


    /*
    if(m_isAdding == true) return;
*/
    m_isCanceling =true;
//std::cout<<"cancel start"<<std::endl;
    //m_selectedNode->removeChildren(0,m_selectedNode->getNumChildren());
    m_selectedNode.clear();
    for(int i = 0; i < m_outLineNodeVector.size(); i++)
    {
        osgwTools::removeNode(m_outLineNodeVector[i]);
    }
    m_outLineNodeVector.clear();

    //

    m_isCanceling =false;
//std::cout<<"cancel end"<<std::endl;
}

//osg::ref_ptr<osgFX::Outline> SelectorManagerOsg::getOutlineNode()
//{
//    return m_outLineNode;
//}
